package com.wksc.framework.impexp.handler.exp;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import com.wksc.framework.impexp.constants.PostfixType;
import com.wksc.framework.impexp.template.ExpTemplate;
import com.wksc.framework.impexp.writer.CsvWriter;
import jxl.read.biff.BiffException;
import jxl.write.WriteException;

/**
 * <p> Title(文件名): ExpCsvHandler.java </p>
 * <p> Description(描述): 导出任务的CSV模式处理类 </p>
 * <p> Copyright(版权): Copyright (c) 2011 </p>
 * <p> Company(公司): 成都四方信息技术有限公司 </p>
 * <p> CreateTime(生成日期)：2011-11-23 下午06:02:15 </p>
 * @author wanglei
 * @version impExp
 */
public class ExpCsvHandler extends BaseExpHandler {
	private static final long serialVersionUID = -3214305587825869748L;

	public ExpCsvHandler(ExpTemplate expTemplate) throws BiffException, FileNotFoundException, IOException {
		super(expTemplate);
	}

	@Override
	public int writeToFile(List<?> data) throws IOException, WriteException,
			BiffException {
		writer.initWriter(createFile(), true);
		int succNum = 0;
		try {
			/* 1.写标题行 */
			writer.writeByRow(expTemplate.getShowedHeader(), 0);
			/* 2.写内容 */
			// 如果模板中合并单元格属性不为空则使用合并单元格的方式写文件
			if (null != expTemplate.getExcelConflate()) {
				succNum = writer.writeByRows(data, 1);
			} else {
				// 写内容并返回
				succNum = writer.writeByRows(data, 1, expTemplate.getWriteOrder());
			}
		} finally {
			writer.close();
		}
		return succNum;
	}

	@Override
	protected void init() throws BiffException, FileNotFoundException,
			IOException {
		writer = new CsvWriter();
	}

	@Override
	public PostfixType getPostfix() {
		return PostfixType.CSV;
	}
}
